﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;

namespace WPFStudy.Converter
{
    /// <summary>
    /// 日期时间转换器
    /// </summary>
    public class DateTimeToStringConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            try
            {
                if (value is DateTime dt)
                {
                    if (parameter == null)
                    {
                        parameter = "yyyy-MM-dd HH:mm:ss";
                    }
                    if (parameter is string par)
                    {
                        return dt.ToString(par);
                    }
                }
            }
            catch
            {
            }
            return value;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return value;
        }
    }
}
